const fs = require('fs') //创建文件、文件夹
const cheerio = require('cheerio') //cheerio爬虫
const iconv = require('iconv-lite') // 编码
const requests = require('sync-request') //node的网络请求
const request = require('request') //利用request模块保存图片

var html = '';
let count = 0 // 记录扒取的图片数量
let imgDirName = '' // 图片存放的目录

let baseUrl = "https://www.lingdianzww.com"
let url = baseUrl + "/ddddfdffd.php?ie=gbk&q=" + encodeURI('满唐华彩'); // 目标网站
// let url = "https://www.baidu.com";

// 拿到网站内容转化成html字符串
// request({
//     url: url,
//     method: "GET",
//     encoding: null//获取的内容不编码，二进制
// }, function (error, response, buffer) {
//     html = iconv.decode(buffer, 'GBK').toString();
//     // console.log(html)
//     // 调用自己的方法
//     search(html)
// });

test()

function test() {
    let bookName = "book/异兽迷城"
    fs.exists(bookName, function (exists) {
        if (exists) {
            console.log("该文件存在！");
        }
        else {
            // 创建文件夹
            fs.mkdir(bookName, err => {
                if (err) {
                    console.log('创建文件夹失败', err);
                    return;
                }
                console.log('创建文件夹成功');
            })
        }
    });
    request({
        url: 'https://www.lingdianzww.com/book/55578/',
        method: "GET",
        encoding: null//获取的内容不编码，二进制
    }, function (error, response, buffer) {
        html = iconv.decode(buffer, 'utf-8').toString();
        // 调用自己的方法
        searchChapter(bookName, html)
    });
}

function search(html) {
    if (html) {
        const $ = cheerio.load(html, { decodeEntities: false });
        // console.log($('body .wrap .so_list .type_show .bookbox'))
        $('body .wrap .so_list .type_show .bookbox').each(function (index, elem) {
            console.log($(elem).find('.bookname').text() + '-' + $(elem).find('.author').text())
            let chapterListUrl = baseUrl + $(elem).find('.bookname a').attr('href')
            let bookName = $(elem).find('.bookname').text()
            console.log("网址：" + chapterListUrl)

            fs.exists(bookName, function (exists) {
                if (exists) {
                    console.log("该文件存在！");
                }
                else {
                    // 创建文件夹
                    fs.mkdir(bookName, err => {
                        if (err) {
                            console.log('创建文件夹失败', err);
                            return;
                        }
                        console.log('创建文件夹成功');
                    })
                }
            });

            // 进入章节目录
            request({
                url: chapterListUrl,
                method: "GET",
                encoding: null//获取的内容不编码，二进制
            }, function (error, response, buffer) {
                html = iconv.decode(buffer, 'utf-8').toString();
                // console.log(html)
                // 调用自己的方法
                searchChapter(bookName, html)
            });
        })
    }
}

function searchChapter(bookName, html) {
    if (html) {
        const $ = cheerio.load(html, { decodeEntities: false });
        console.log($('body .wrap .listmain'))
        let i = 0
        $('body .wrap .listmain dl dd').each(function (index, elem) {
            if (i == 0) {
                let chapterName = $(elem).text().trim()
                console.log(chapterName)
                let chapterUrl = baseUrl + $(elem).find('a').attr('href')
                console.log("网址：" + chapterUrl)

                // 进入章节
                request({
                    url: chapterUrl,
                    method: "GET",
                    encoding: null//获取的内容不编码，二进制
                }, function (error, response, buffer) {
                    html = iconv.decode(buffer, 'utf-8').toString();
                    // console.log(html)
                    getContent(bookName, chapterName, html)
                });
            }
            // i++
        })
    }
}

function getContent(bookName, chapterName, html) {
    if (html) {
        const $ = cheerio.load(html, { decodeEntities: false });
        chapterName = chapterName.replace(/(\s)+/g,"")
        chapterName = chapterName.replace(/(\?)+/g,"")
        let fileName = bookName + "/" + chapterName + '.txt'
        let content = $('#content').text().trim()
        content = content.replace(/(\n)+/g,"wrap")
        content = content.replace(/(\s)+/g,"")
        content = content.replace(/(wrap)+/g,"\n\n")
        // console.log(content)
        fs.writeFile(fileName, content, (err) => {
            if (err) throw err;
            console.log('文本文件已创建并写入数据');
        });
    }
}